//import axios from 'axios'

//获取cookie、
export function getCookie(name) {
 var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
 if (arr = document.cookie.match(reg))
  return (arr[2]);
 else
  return null;
}
 
//设置cookie,增加到vue实例方便全局调用
export function setCookie (c_name, value, expiredays) {
 var exdate = new Date();
 exdate.setDate(exdate.getDate() + expiredays);
 document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
};
 
//删除cookie
export function delCookie (name) {
 var exp = new Date();
 exp.setTime(exp.getTime() - 1);
 var cval = getCookie(name);
 if (cval != null)
  document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
};


export function getQueryString(name) {
	var reg = new RegExp("(^|&\?)" + name + "=([^&]*)(&|$)");
	var r = window.location.href.match(reg);
	if(r != null) {
		return unescape(r[2]);
	}
	return null;
}

//传bb
//https://www.abc.com/aaa?cc=1&bb=2&aa=1
//=> 
//https://www.abc.com/aaa?cc=1&aa=1
export function cutQueryString(name, where ,oldUrl, newpage) {
	var url = oldUrl == undefined ? window.location.href : oldUrl;
	var reg ;
	if(where == 'before'){
		reg = new RegExp("(^|&\?)" + name + "=([^#]*)(#|$)");
	}else if(where == 'page'){
		reg = new RegExp("#/" + name + "?");
	}else{
		reg = new RegExp("(^|&\?)" + name + "=([^&]*)(&|$)");
	}
	console.log(reg);
	var r = url.match(reg);
	console.log(r);
	if(r != null) {
		console.log(r);
		var urls = url.split(r[0]);
		console.log(urls);
		var result = '';
		if(r[3] != ''){
			if(where == 'before'){
				result = urls[0] + '#' + urls[1];
			}else if(where == 'page'){
				result = urls[0] + '#/' + newpage + urls[1];
			}else{
				result = urls[0] + '&' + urls[1];
			}
		}else{
			result = urls[0] + urls[1];
		}
		return result;
	}
	return url;
}




export function str2date(strDate){

	if(typeof strDate === 'string')
	{
		var objDate = new Date(Date.parse(strDate.replace(/-/g, "/")));

		return objDate;
	}
	else
	{
		return '错误格式';
	}
	
}


export function date2str(objDate){

	if (typeof objDate === 'object') {
		var y = objDate.getFullYear();
		var m = objDate.getMonth() + 1;
		var d = objDate.getDate();
		m = m < 10? '0' + m: m;
		d = d < 10? '0' + d: d;
		return y + '-' + m + '-' + d;
	}
	else {
		return '错误格式';
	}
	
}


export function cutImage (obj) {

	

	var width = obj.w;
	var height = obj.h;

	if(width < 400 || height < 400 ){
		var x = '';
		var y = '';
		var w = '';
		var h = '';

		if(width > height){
			x = (width - height) * 0.5;
			y = 0;
			w = height;
			h = height;
		}else{
			x = 0;
			y = (height - width) * 0.5;
			w = width;
			h = width;
		}

		return obj.url + '?x-oss-process=image/crop,' + 'x_' + parseInt(x) + ',y_' + parseInt(y) + ',w_' + parseInt(w) + ',h_' + parseInt(h);
	}else{
		return obj.url + '?x-oss-process=style/list';
	}

	
	//return obj.url + '?x-oss-process=image/crop,' + 'x_' + parseInt(x) + ',y_' + parseInt(y) + ',w_' + parseInt(w) + ',h_' + parseInt(h);
};



export function changeImg(url, what){
	return url + '?x-oss-process=style/' + what;
}


export function imgUtil(src) {
  return new Promise((resolve, reject) => {
      let img = new Image();
      img.setAttribute('crossOrigin', 'Anonymous');
      img.onload = function () {
        resolve(img)
      }
      img.onerror = () => {
      	console.log('the error in imgUtil');
		reject(new Error('That image was not found.:' + src.length))
	  }
      img.src = src;
  })
}


function dataURL2blob(dataurl) {
    var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
    while (n--) {
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
}

/**
 * 创建新的URL 对象表示指定的 File 对象或 Blob 对象。
 * @param {string} dataURL  base64
 */
function dataURL2ObjUrl(dataURL) {
    window.URL = window.URL || window.webkitURL
    if (window.URL && URL.createObjectURL) {
        // dataURL2blob 此方法需额外定义
        var blob = dataURL2blob(dataURL)
        return URL.createObjectURL(blob)
    }
    return dataURL
}

export function imgUtil1(url) {
  return new Promise((resolve, reject) => {
      let img = new Image();
      img.setAttribute('crossOrigin', 'Anonymous');

      	var objectURL = null
        if (url.match(/^data:(.*);base64,/) && window.URL && URL.createObjectURL) {
            objectURL = URL.createObjectURL(dataURL2blob(url))
            url = objectURL
            console.log(url);
        }
        img.onload = () => {
            //objectURL && URL.revokeObjectURL(objectURL)
            resolve(img)
        }
        img.onerror = () => {
            reject(new Error('That image was not found.:' + url.length))
        }
        img.src = url;


   //    img.addEventListener('load', function() {
		 //    resolve(img);
	  // }, false);
   //    img.src = src;
  })
}



//校验身份证
export function identityCodeValid(code){

	var city={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外 "};
    var tip = "";
    var pass= true;
    
    if(!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)){
        tip = "身份证号格式错误";
        pass = false;
    }
    
   else if(!city[code.substr(0,2)]){
        tip = "身份证地址编码错误";
        pass = false;
    }
    else{
        //18位身份证需要验证最后一位校验位
        if(code.length == 18){
            code = code.split('');
            //∑(ai×Wi)(mod 11)
            //加权因子
            var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
            //校验位
            var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
            var sum = 0;
            var ai = 0;
            var wi = 0;
            for (var i = 0; i < 17; i++)
            {
                ai = code[i];
                wi = factor[i];
                sum += ai * wi;
            }
            var last = parity[sum % 11];
            if(parity[sum % 11] != code[17]){
                tip = "身份证校验位错误";
                pass =false;
            }
        }
    }
    //if(!pass) alert(tip);
    return {
    	pass,
    	tip,
    };
	
}

export function dateDiff (sDate1,  sDate2){    //sDate1和sDate2是2006-12-18格式  
   var  aDate,  oDate1,  oDate2,  iDays  
   aDate  =  sDate1.split("-")  
   oDate1  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])    //转换为12-18-2006格式  
   aDate  =  sDate2.split("-")  
   oDate2  =  new  Date(aDate[1]  +  '-'  +  aDate[2]  +  '-'  +  aDate[0])  
   iDays  =  parseInt((oDate2 - oDate1)  /  1000  /  60  /  60  /24)    //把相差的毫秒数转换为天数  
   return  iDays  
}
		// ,

		// date2str : function (objDate){

		// 	if (angular.isDate(objDate)) {
		// 		var y = objDate.getFullYear();
		// 		var m = objDate.getMonth() + 1;
		// 		var d = objDate.getDate();
		// 		m = m < 10? '0' + m: m;
		// 		d = d < 10? '0' + d: d;
		// 		return y + '-' + m + '-' + d;
		// 	}
		// 	else {
		// 		return '错误格式';
		// 	}

		// 	// if(angular.isDate(objDate))
		// 	// {
		// 	// 	var y = objDate.getFullYear();
		// 	// 	var m = objDate.getMonth();
		// 	// 	var d = objDate.getDate();

		// 	// 	return y + '-' + (m + 1) + '-' + d;
		// 	// }
		// 	// else
		// 	// {
		// 	// 	return '错误格式';
		// 	// }
		// }
